package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.vo.VideoInfo;
import com.atguigu.eduservice.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author 老王
 * @since 2020-05-20
 */
@Api(description="课时管理相关接口")
@CrossOrigin //跨域
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;

    @ApiOperation(value = "新增课时")
    @PostMapping("/save")
    public R save(@RequestBody VideoInfo videoInfo){
        videoService.saveVideoInfo(videoInfo);
        return R.ok();
    }

    @ApiOperation(value = "更新课时")
    @PostMapping("update")
    public R update(@RequestBody VideoInfo videoInfo){
        videoService.updateVideoInfo(videoInfo);
        return R.ok();
    }

    @ApiOperation(value = "根据id删除小节，同时调用vod服务删除视频")
    @ApiImplicitParam(name = "videoId", value = "课时ID", required = true)
    @DeleteMapping("{videoId}")
    public R delete(@PathVariable String videoId){
        boolean result = videoService.removeVideoById(videoId);
        if(result){
            return R.ok();
        }else{
            return R.error().message("删除失败");
        }
    }

    @ApiOperation(value = "根据ID查询课时")
    @ApiImplicitParam(name = "videoId", value = "课时ID", required = true)
    @GetMapping("/{videoId}")
    public R getById(@PathVariable String videoId){

        EduVideo video = videoService.getVideoById(videoId);
        return R.ok().data("video", video);
    }

}

